<?php

/**
 * This file is part of ossecdb-extjs.
 * 
 * Copyright (C) 2011 Brendan Johnston
 * 
 * Contact: brendan@johnston.net.au
 * 
 * Project: http://code.google.com/p/ossecdb-extjs/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

require('_inc/_all.php');

try
{
	if (isset($_REQUEST['node']))
	{
		$node = $_REQUEST['node'];
		$checked = intval($_REQUEST['checked']) == 1;

		if ($node == 'locations')
		{
			$col_host = $queries->location_col['host'];

			$locations = ossec_db::query("
				select 
					distinct 
						$col_host as `id`,
						$col_host as `text`
				from 
				$queries->location_from
				order by
					$col_host;");

			foreach ($locations as $location)
			{
				$location->checked	= $checked;
				$location->leaf		= false;
				$location->type		= 'location';
				$location->cls		= 'location';
			}

			echo json_encode($locations);
		}
		else
		{
			$hostname = ossec_db::db()->quote($node);

			$col_user = $queries->alert_col['user'];
			$col_host = $queries->alert_col['loc_host'];

			$users = ossec_db::query("
				select
					distinct 
						$col_user as `text`
				from
				$queries->alert_from
				where
					$col_host = $hostname
				order by
					$col_user;");

			unset($col_user);
			unset($col_host);

			foreach ($users as $user)
			{
				$user->checked		= $checked;
				$user->leaf			= true;
				$user->type			= 'user';
				$user->cls			= 'user';
			}

			//:~

			$paths = array();

			/**
			$users = array(
				(object) array(
					'text'		=> 'Users',
					'children'	=> $users,
					'leaf'		=> false,
					'type'		=> 'users',
					'cls'		=> 'users'
				)
			);

			$col_host = $queries->location_col['host'];
			$col_path = $queries->location_col['path'];

			$paths = ossec_db::query("
				select 
					$col_path as `text`
				from 
				$queries->location_from
				where
					$col_host = $hostname
				order by
					$col_path;");

			unset($col_host);
			unset($col_path);

			foreach ($paths as $path)
			{
				$path->checked	= $checked;
				$path->leaf		= true;
				$path->type		= 'path';
				$path->cls		= 'path';

				if (strpos($path->text, '/') === false && strpos($path->text, '\\') === false)
				{
					$path->cls .= ' proc';
				}
				else
				{
					$path->cls .= ' file';
				}
			}
			*/

			echo json_encode(
				array_merge($users, $paths)
			);
		}
	}

	ossec_db::close();

}
catch (PDOException $e)
{
	echo 'Error connecting to DB!: '.$e->getMessage();
	exit();
}